package com.sixbro.caicaitv.widget;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sixbro.caicaitv.R;
import com.sixbro.caicaitv.bean.ChannelBean;
import com.sixbro.caicaitv.util.LogUtils;

public class CcLivePlayer extends FrameLayout {

    private ViewHolder mViewHolder;
    private ChannelBean mChannelBeanCur;

    public CcLivePlayer(@NonNull Context context) {
        super(context);
        init(context);
    }

    public CcLivePlayer(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CcLivePlayer(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public CcLivePlayer(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.cc_live_player, this);
        mViewHolder = new ViewHolder(view);
        //初始化流播放器
        mViewHolder.vvVideo.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mediaPlayer, int what, int extra) {
                LogUtils.d("onInfo:" + what);
                if (what == 5003 || what == 710 || what == 703 || what == 701) {
                    LogUtils.d(mChannelBeanCur.getName() + " 缓冲中...");
                    mViewHolder.pbLoading.setVisibility(View.VISIBLE);
                } else if (what == 706) {
                    showErrorView();
                } else {
                    LogUtils.d(mChannelBeanCur.getName() + " 缓冲好了");
                    mViewHolder.pbLoading.setVisibility(View.GONE);
                    mViewHolder.flytChannelInfo.setVisibility(View.INVISIBLE);
                }
                return true;
            }
        });
        mViewHolder.vvVideo.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                LogUtils.d("onError:" + what);
                if (what != 804) {
                    showErrorView();
                }
                return true;
            }
        });
    }

    //错误页
    private void showErrorView() {
        if (mChannelBeanCur.hasNextUrl()) {
            mChannelBeanCur.moveToNextUrl();
            startPlay(mChannelBeanCur, true);
        } else {
            mViewHolder.tvError.setText("直播源错误");
            mViewHolder.tvError.setVisibility(View.VISIBLE);
            mViewHolder.pbLoading.setVisibility(View.GONE);
        }
    }

    public void startPlay(ChannelBean channelBean, boolean immediately) {
        mChannelBeanCur = channelBean;
        if (mChannelBeanCur != null) {
            mViewHolder.vvVideo.pause();
            mViewHolder.vvVideo.setVisibility(View.INVISIBLE);
            mViewHolder.vvVideo.stopPlayback();
            mViewHolder.tvError.setVisibility(View.INVISIBLE);
            mViewHolder.pbLoading.setVisibility(View.VISIBLE);
            mViewHolder.flytChannelInfo.setVisibility(View.VISIBLE);
            mViewHolder.tvChannelName.setText(mChannelBeanCur.getName());
            mViewHolder.tvChannelInfo.setText("源地址：" + mChannelBeanCur.getUrl());
            LogUtils.d(mChannelBeanCur.getName() + " " + mChannelBeanCur.getUrl());
            if (immediately) {
                playImmediately();
            } else {
                mViewHolder.flytChannelInfo.removeCallbacks(runPlay);
                mViewHolder.flytChannelInfo.postDelayed(runPlay, 100);
            }
        } else {
            mViewHolder.flytChannelInfo.setVisibility(View.VISIBLE);
            mViewHolder.tvChannelName.setText(null);
            mViewHolder.tvError.setText("直播源为空");
        }
    }

    private Runnable runPlay = new Runnable() {
        @Override
        public void run() {
            playImmediately();
        }
    };

    private void playImmediately() {
        mViewHolder.vvVideo.setVisibility(VISIBLE);
        mViewHolder.vvVideo.setVideoURI(Uri.parse(mChannelBeanCur.getUrl()));
        mViewHolder.vvVideo.start();
    }

    static
    class ViewHolder {
        MyVideoView vvVideo;
        TextView tvChannelName;
        TextView tvChannelInfo;
        TextView tvError;
        FrameLayout flytChannelInfo;
        ProgressBar pbLoading;

        ViewHolder(View view) {
            vvVideo = view.findViewById(R.id.vv_video);
            tvChannelName = view.findViewById(R.id.tv_channel_name);
            tvChannelInfo = view.findViewById(R.id.tv_channel_info);
            tvError = view.findViewById(R.id.tv_error);
            flytChannelInfo = view.findViewById(R.id.flyt_channel_info);
            pbLoading = view.findViewById(R.id.pb_loading);
        }
    }
}
